<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
	<%@ page import="Logic.Authorizer"%>
	<%@ page import="java.sql.*" %>
	<%@ page import="servlet.ProcessesDropDown,Database.DBConnection"%>
	<%@ page import="java.io.*" %> 
	<%@ page import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
	if (session.getAttribute("name") == null) {
		response.sendRedirect("login.jsp");
	} else {
		if (!Authorizer.checkRole("2", session.getAttribute("role")
				.toString())) {
			response.sendRedirect("nopermission.jsp");
		}
	}
%>
<jsp:include page="t_header.jsp" />

<!--  <div class="breadcrumbs_container">
	<article class="breadcrumbs"> <a href="Proc_NewProcess.jsp">Manage Processes</a>
	<div class="breadcrumb_divider"></div>
	<a class="current">Delete Process</a> 
	</article>
</div> -->
<jsp:include page="header.html" />

<!-- Declare sidebar -->
<jsp:include page="t_sidebarProcess.jsp" />

<!-- Main Content -->
<body>
	<section id="main" class="column">
<article class="module width_3_quarter">
	<form id="DeleteProcess" name="DeleteProcess" onsubmit="CFMDelete();">
	<header><h3>Select a Process to delete</h3></header>
	<div class="module_content">
	<fieldset>
	<label>Process Name</label><select name="PN" id="PN" style="width: 80%">
						<%
							ProcessesDropDown PDD = new ProcessesDropDown(out);
							PDD.doGet(request, response);
							
						%>
					</select><br></fieldset></div>
<footer>
<div class="submit_link">
	<input id="Delete" name="Delete" type="submit" value="Delete" class="alt_btn">
	</div>
	</footer>
	<input id="answer" name="answer" type="hidden"/>
	
	<%
	String answer = request.getParameter("answer");
  	 if(answer == null || "".equals(answer))answer = "false";
  	 	if("true".equals(answer)){
  	 		
 System.out.println("User confirmed to delete.");
 String VarSelected=request.getParameter("PN");
 try
{

String connectionURL1st = "jdbc:mysql://localhost:3306/qmsdb";
Connection connection1st = null;
Statement statement1st = null;
ResultSet rs1st = null;
	
	connection1st=DBConnection.connect(true); 
statement1st = connection1st.createStatement();
String QueryString1st = "SELECT * from qmsdb.processes WHERE ProcessName='"+VarSelected+"'";
rs1st = statement1st.executeQuery(QueryString1st);
List<String> tempProcessID=new ArrayList<String>();
while (rs1st.next())
{
	tempProcessID.add(Integer.toString(rs1st.getInt("idProc")));
}			 
for(int i=0;i<tempProcessID.size();i++)
 {
			
			Connection con=null,con1=null; 
  			PreparedStatement stmt=null,stmt1=null; 
  			PreparedStatement ps=null,ps1=null; 
  			ResultSet rs=null,rs1=null; 
  			String sql="",sql1="",a=""; 
  			
  			con=DBConnection.connect(true); 
  			int j1=0; 
  			try
  			{ 
  				sql="DELETE FROM qmsdb.forms WHERE idProc='"+tempProcessID.get(i)+"'"; 
  				stmt = con.prepareStatement(sql);
  				j1=stmt.executeUpdate();
  				if(j1!=0) 
  	  			{ 				
  					System.out.println("Form of Procedure ID:'"+tempProcessID.get(i)+"' deleted!");
  					
  	  			}
  		    }
  			catch(Exception ex) 
  			{ 
  				ex.printStackTrace(); 
  			} 
			finally{
	
			}
 }
 System.out.println(tempProcessID.get(1).toString());
// close all the connections.
rs1st.close();
statement1st.close();
connection1st.close();
}
catch(Exception ex)
{
}
finally
{
}
			Connection con=null,con1=null; 
  			PreparedStatement stmt=null,stmt1=null; 
  			PreparedStatement ps=null,ps1=null; 
  			ResultSet rs=null,rs1=null; 
  			String sql="",sql1="",a=""; 
  			
  			con=DBConnection.connect(true); 
  			int j1=0; 
  			try
  			{ 
  				sql="DELETE FROM qmsdb.processes WHERE ProcessName='"+VarSelected+"'"; 
  				stmt = con.prepareStatement(sql);
  				j1=stmt.executeUpdate();
  				if(j1!=0) 
  	  			{ 				
  					System.out.println("Operation Completed Successfully! ");
  					  					%>
  					  					<script>
  					window.location="Proc_Process.jsp";
  					  					</script>
  					<%	
  	  			} 
  		    }
  			catch(Exception ex) 
  			{ 
  				ex.printStackTrace(); 
  			} 
			finally{
	
			}	
		
 }
 	 		
  	 	else
  	 	{
  	 	
  	 	}
	 
   		 
%>
    </form>
    </article>
<script type="text/javascript">
function CFMDelete(){
var TempVal=document.getElementById('PN');
var selectedText = TempVal.options[TempVal.selectedIndex].text;
var answer = confirm ("Are you sure you want to continue deleting \n("+selectedText+")? \nProcedures and forms that are under this process will also be deleted! ");
  if (answer){
              document.DeleteProcess.answer.value = "true";  
              document.DeleteProcess.submit();
            
              return true;
    }else 
    alert("Please choose carefully before deleting! ");
    return false;
}

</script>
<div class="spacer"></div>
</section>
	<!-- Declare closing tags -->
	<jsp:include page="t_footer.jsp" />



